package ch.gibb.learnit.client.controllers;

import ch.gibb.learnit.client.mapping.ActionInterface;
import ch.gibb.learnit.client.mapping.WebPageMapper;
import ch.gibb.learnit.client.util.Messages;
import ch.gibb.learnit.client.util.ServiceAccess;
import ch.gibb.learnit.client.util.soap.ObjectFactory;
import ch.gibb.learnit.client.util.soap.User;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Abstract controller which defines some general functions
 *
 * @author Bernard Jaquet
 * @version 1.0
 */
public abstract class MasterAction implements ActionInterface {
    /**
     * Title of the page
     */
    protected String title;
    private HttpSession session;
    private HttpServletResponse response;

    /**
     * String to access userId and password in Session
     */
    private static final String USER_SESSION = "USER_SESSION";
    private static final String PASSWORD_SESSION = "PASSWORD_SESSION";

    /**
     * Object factory for SOAP elements
     */
    protected ObjectFactory factory = new ObjectFactory();

    /**
     * Instance of the ServiceAccess
     */
    protected ServiceAccess client = new ServiceAccess();

    /**
     * Fe messages groups
     */
    private Messages errors = new Messages();
    private Messages warnings = new Messages();
    private Messages info = new Messages();
    private Messages success = new Messages();
    private Messages hints = new Messages();

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }


    /**
     * Opens a Session with a user
     *
     * @param id
     * @param password
     */
    protected void loginUser(int id, String password) {
        session.setAttribute(USER_SESSION, id);
        session.setAttribute(PASSWORD_SESSION, password);
    }

    /**
     * Returns the id of the logged user
     *
     * @return
     */
    protected int getLogedInUser() {
        if (session.getAttribute(USER_SESSION) == null)
            return -1;
        return Integer.valueOf(session.getAttribute(USER_SESSION).toString());
    }

    /**
     * Checks if a user is logged in
     *
     * @return
     */
    protected boolean loginCheck() {
        if (getLogedInUser() == -1)
            return false;
        return true;
    }

    /**
     * Loges out the active user
     */
    protected void logoutUser() {
        session.removeAttribute(USER_SESSION);
        session.removeAttribute(PASSWORD_SESSION);
    }

    /**
     * Redirects th an other page
     *
     * @param page Name of the page without the rest of the url
     * @return Exit code to be returned
     * @throws IOException
     */
    protected int redirect(String page) throws IOException {
        response.sendRedirect(WebPageMapper.BASE_URL + page);
        return WebPageMapper.REDIRECT_CODE;
    }

    /**
     * Generates the User object used to get information from the SOAP service
     * @return
     */
    protected User getUser() {
        User user = client.getUser(getLogedInUser());
        user.setPw(factory.createUserPw(session.getAttribute(PASSWORD_SESSION).toString()));
        return user;
    }

    public void setSession(HttpSession session) {
        this.session = session;
    }

    public void setResponse(HttpServletResponse response) {
        this.response = response;
    }

    public Messages getErrors() {
        return errors;
    }

    public Messages getWarnings() {
        return warnings;
    }

    public Messages getInfo() {
        return info;
    }

    public Messages getSuccess() {
        return success;
    }

    public Messages getHints() {
        return hints;
    }
}
